﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Views
{
    public partial class PopupListContacts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ShowContact();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            var listContact = new List<Contact>();
            var find = new ContactBiz();
            listContact = find.getAllContactByKey(txtFirstName.Text.ToString(), txtSurName.Text.ToString(),true);
            if (listContact != null)
            {
                GVSearchContact.DataSource = listContact;
                DataBind();
            }
        }
        private void ShowContact()
        {
            var listContact = new List<Contact>();
            var find = new ContactBiz();
            listContact = find.getAllContactByKey(string.Empty, string.Empty,true);
            if (listContact != null)
            {
                GVSearchContact.DataSource = listContact;
                DataBind();
            }

        }
        protected void btSelect_Click(object sender, EventArgs e)
        {
            string selectedValue = Request.Form["MyRadioButton"];

            if (Convert.ToInt32(selectedValue) > 0)
            {
                var contact = new Contact();
                var getbyId = new ContactBiz();
                contact = getbyId.getById(Convert.ToInt32(selectedValue));
                txtFirstName.Text = Convert.ToString(contact.FirstName);
                txtSurName.Text = Convert.ToString(contact.SurName);
                hdnContactId.Value = Convert.ToString(contact.ContactId);
             //   hdnManagerId.Value = Convert.ToString(contact.ManagerId);
                Session["contact"] = txtFirstName.Text + ' ' + txtSurName.Text;
                StringBuilder script = new StringBuilder();
                script.Append("<script type='text/javascript'>");

                script.AppendFormat("window.opener.document.getElementById('txtContact').value  = '{0}';",
                    txtFirstName.Text + ' ' + txtSurName.Text);
                script.AppendFormat("window.opener.document.getElementById('hdnContactId').value  = '{0}';",
                    hdnContactId.Value);
                /*script.AppendFormat("window.opener.document.getElementById('hdnManagerId').value  = '{0}';",
                    hdnManagerId.Value);*/
                script.Append("this.close();");
                script.Append("</script>");
                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(),
                    false);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "UserDialogScript",
                    "alert(\"Select Value\");", true);
            }
        }        

        protected void GVSearchContact_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVSearchContact.PageIndex = e.NewPageIndex;
            ShowContact();
        }
    }
}